import 'package:flutter/material.dart';
import 'layers/index.dart';
import 'photo/gallery_page.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const GalleryPage(),
    );
  }
}

class MeasurePage extends StatefulWidget {
  const MeasurePage({super.key});

  @override
  State<MeasurePage> createState() => _MeasurePageState();
}

class _MeasurePageState extends State<MeasurePage> {
  final GlobalKey _canvasKey = GlobalKey();
  Size? _canvasSize;
  final List<Layer> _layers = [];
  int _ellipseId = 0;
  int _lineId = 0;

  void _addEllipse() {
    if (_canvasSize == null) return;
    final center = Offset(_canvasSize!.width / 2, _canvasSize!.height / 2);
    final layer = EllipseLayer(center, 100, 80, 0)
      ..id = 'ellipse_${_ellipseId++}';
    setState(() => _layers.add(layer));
  }

  void _addLine() {
    if (_canvasSize == null) return;
    final centerY = _canvasSize!.height / 2;
    final start = Offset(50, centerY);
    final end = Offset(_canvasSize!.width - 50, centerY);
    final layer = LineLayer(start, end)..id = 'line_${_lineId++}';
    setState(() => _layers.add(layer));
  }

  int _textId = 0;

  void _addTextLayer() {
    if (_canvasSize == null) return;
    final center =
        Offset(_canvasSize!.width / 2 - 40, _canvasSize!.height / 2 - 10);
    final layer = TextLayer(position: center, text: '文本 ${_textId + 1}')
      ..id = 'text_${_textId++}';
    setState(() => _layers.add(layer));
  }

  void _onLayerMetricChanged(String id, dynamic newValue) {
    // 你可以在这里处理 UI 更新、显示 Toast、状态记录等
    debugPrint("Layer $id metric changed: $newValue");
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      final renderBox =
          _canvasKey.currentContext?.findRenderObject() as RenderBox?;
      if (renderBox != null) {
        _canvasSize = renderBox.size;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('图层测量')),
      backgroundColor: Colors.white,
      body: Center(
        child: Container(
          key: _canvasKey,
          width: 400,
          height: 400,
          color: Colors.black,
          child: LayerCanvas(
            layers: _layers,
            onLayerMetricChanged: _onLayerMetricChanged,
          ),
        ),
      ),
      floatingActionButton: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          FloatingActionButton(
            onPressed: _addEllipse,
            child: const Icon(Icons.circle_outlined),
          ),
          const SizedBox(height: 10),
          FloatingActionButton(
            onPressed: _addLine,
            child: const Icon(Icons.linear_scale),
          ),
          const SizedBox(height: 10),
          FloatingActionButton(
            onPressed: _addTextLayer,
            child: const Icon(Icons.text_fields),
          ),
        ],
      ),
    );
  }
}
